package com.excel.service.impl;



import com.excel.mapper.AdvertisementExcelForCollectionMapper;
import com.excel.service.ScreenInfoExportService;
import com.excel.service.gernerate.OrderScrenAdvertisementCollection;
import com.excel.service.gernerate.OrderScrenAdvertisementLongExcel;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.*;


/**
 * @author guobaolin
 * @date 2016年8月17日
 * @time 下午4:04:48
 */
@Service("screenInfoExportService")
public class ScreenInfoExportServiceImpl implements ScreenInfoExportService {




    @Autowired
    private AdvertisementExcelForCollectionMapper advertisementExcelForCollectionMapper;


    @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;

//    public List<OrderScreenExportExcel> queryExportExcel(String orderId, Long clientId, String year, Boolean isHistory) {
//        return exportExcel.query(orderId, clientId, year, isHistory);
//    }

    /**
     * 查询对应订单日期
     */
//    public List<OrderSimpleDate> queryForDate(String orderId, String year, Boolean isHistory) {
//        return advertisementExcelMapper.queryForDate(orderId, year, isHistory);
//    }

    /**
     * 查询对应的广告报价单 + 媒体点位表
     */
//    public List<OrderScrenAdvertisementLongExcel> queryForAdvertisement(String orderId, Long clientId, String year, Boolean isHistory) {
//        return advertisementExcelMapper.queryForAdvertisement(orderId, year, isHistory);
//    }

    /**
     * 查询对应广告报价单的集合
     */
    public List<OrderScrenAdvertisementCollection> queryForAdCollection(String orderId, String year, Boolean isHistory) {
        return advertisementExcelForCollectionMapper.queryForAdCollection(orderId, year, isHistory);
    }

    /**
     * 判断当前订单是否已经完成
     *
     * @param orderId
     * @param year
     * @return
     */
    public Boolean isHistoryByQueryOrder(String orderId, String year) {

        if (null != advertisementExcelForCollectionMapper.queryOrderByOrderId(orderId)) {
            //表明订单还存在ad_order
            return true;
        } else if (null != advertisementExcelForCollectionMapper.queryOrderByOrderIdHistory(orderId, year)) {
            //订单存在历史表里
            return false;
        }

        return false;
    }



    /**
     * 导出excel的广告报价表
     *
     * @param dataset
     * @param dayNum
     * @param betweenDay
     * @param out
     * @param pattern
     * @param collectionset
     */
    public void exportExcelWithAdvertisementTemplate(Collection<OrderScrenAdvertisementLongExcel> dataset, int dayNum,Float discount,Double actualPrice,
                                                     String betweenDay,Writer out, String pattern, Collection<OrderScrenAdvertisementCollection> collectionset) {
        try {

           Template template = freeMarkerConfigurer.getConfiguration().getTemplate("advertisementTest.ftl");
//            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("advertisementTest_detail.ftl");

            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DAY_OF_MONTH, 10);

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("confirmDate", c.getTime());
            map.put("betweenDay", betweenDay);
            map.put("orderDay", dayNum);
            //orderScrenAdvertisementLongExcels
//            map.put("totalDetailExcels", dataset);
            map.put("orderScrenAdvertisementLongExcels", dataset);
            map.put("orderScrenAdvertisementCollections", collectionset);
            map.put("map", "string");
            map.put("pattern", pattern);
            map.put("discount",discount);
            map.put("actualPrice",actualPrice);

           template.process(map, out);
//            POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(""));
//            String path = KeyFindValue.class.getClassLoader().getResource("").toURI().getPath();
//            String path_excel = path.substring(0, path.lastIndexOf("WEB-INF")) + File.separator + "WEB-INF" + File.separator + "template" + File.separator + "advertismet_price.xlsx";
//            File adexcels = new File(path_excel);
//            XSSFWorkbook xssf = new XSSFWorkbook(adexcels);//new XSSFWorkbook(poifsFileSystem);
//            List<String> sheetNames = new ArrayList<>(0);
//            sheetNames.add("媒体点位表");
//            sheetNames.add("广告报价表");
//            xssf = ExcelPOILongUtil.generateADExcelByModule(xssf,sheetNames,map);
//
//            xssf.write(fout);
//            fout.close();

        } catch (Exception  e) {
            e.printStackTrace();
        }
    }


//    public void exportExcelFromTemplate(Collection<OrderScreenExportExcel> dataset, int dayNum,
//                                        String betweenDay, Writer out, String pattern) {
//        try {
//            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("manualMediaExport.ftl");
//            Map<String, Object> map = new HashMap<>();
//            map.put("betweenDay", betweenDay);
//            map.put("orderDay", dayNum);
//            map.put("orderScreenExportExcels", dataset);
//            map.put("map", new KeyFindValue());
//            map.put("pattern", pattern);
//
//            template.process(map, out);
//        } catch (IOException | TemplateException e) {
//
//            e.printStackTrace();
//        }
//    }

//    public boolean queryHasExcludeScreen(String orderId, int sequence) {
//        return advertisementExcelMapper.loadCountByExclude(orderId, sequence, true) > 0;
//    }

    @Override
    public Float queryDiscount(String orderId) {

        Float discount = advertisementExcelForCollectionMapper.queryDiscount(orderId);

        if (null == discount ){
            discount = 0.00f;
        }
        return discount;
    }

    public Double queryOrderActualPrice(String orderId) {
        return advertisementExcelForCollectionMapper.queryOrderActualPrice(orderId);
    }

    @Override
    public Double queryAdServicePrice(String orderId, String times, Boolean isHistory) {
        return advertisementExcelForCollectionMapper.queryAdServicePrice(orderId,times,isHistory);
    }

}
